const state = {
  // 搜索到的商品信息
  searchInfo: [],
}

const getters = {}

const mutations = {
  // 将查询到的数据保存到vuex中
  searchShop(state, shopinfo) {
    console.log(state, shopinfo)
    state.searchInfo = shopinfo
    console.log(state)
  },

  // 离开页面时将搜索的数据清空
  delsearchShop(state) {
    state.searchInfo = []
  },
  // 读取时
  userInit(state) {
    let userInfo = JSON.parse(localStorage.getItem("UserShopInfo"))
    if (userInfo) {
      state.loginStatus = true
      state.token = userInfo.token
      state.userInfo = userInfo
    }
    return userInfo
  },

  // 退出登录
  loginOut(state) {
    console.log("退出方法执行了")
    state.loginStatus = false
    state.token = null
    state.userInfo = {}
    localStorage.removeItem("UserShopInfo")
  },
  decrementCount(state, payload) {
    state.count -= payload.amout
  },
}

const actions = {
  incrementCountAsync({ commit }) {
    setTimeout(() => {
      commit("incrementCount")
    }, 2000)
  },
  decrementCountAsync({ commit }, payload) {
    setTimeout(() => {
      commit("decrementCount", payload)
    }, 1000)
  },
}

export default {
  state,
  getters,
  mutations,
  actions,
}
